package io.renren.dao;

import io.renren.common.dao.BaseDao;
import io.renren.entity.TradermEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * ${comments}
 *
 * @author Sen 524227263@qq.com
 * @since 1.0.0 2024-05-21
 */
@Mapper
public interface TradermDao extends BaseDao<TradermEntity> {

    @Select("select count(1) as num from( " +
            "select " +
            "MakeCardVouchersM.CardVouchersRuleM_FK, " +
            "MakeCardVouchersD.MakeCardVouchersDOID, " +
            "MakeCardVouchersM.CardVouchersType, " +
            "(case when CardVouchersRuleM.CardNature='1' then '代金卡' " +
            " when CardVouchersRuleM.IsMakeDiffPrice='true' then '需补差' else '' end) as CardNature, " +
            "MakeCardVouchersD.CardVouchersCode, " +
            "GoodsM.Name as CardName, " +
            "(case when MakeCardVouchersD.SentFlag = '0' then '未发卡' " +
            "when MakeCardVouchersD.SentFlag= '1' then '已发卡' " +
            "when MakeCardVouchersD.SentFlag = '2' and (case when convert(varchar(10),MakeCardVouchersD.EndDate,108)='00:00:00' " +
            "     then DATEADD(day,1,MakeCardVouchersD.EndDate) " +
            "     else MakeCardVouchersD.EndDate end) > GETDATE() " +
            "              then '使用中' " +
            "when MakeCardVouchersD.SentFlag = '3' then '已用完' " +
            "when MakeCardVouchersD.SentFlag = '4' then '已终止' " +
            "when MakeCardVouchersD.SentFlag= '5' then '已作废' " +
            "when MakeCardVouchersD.SentFlag= '6' then '已冻结' " +
            "when MakeCardVouchersD.SentFlag = '2' and (case when convert(varchar(10),MakeCardVouchersD.EndDate,108)='00:00:00' then DATEADD(day,1,MakeCardVouchersD.EndDate)else MakeCardVouchersD.EndDate end) < GETDATE() then '已过期' " +
            "else '' end) as SentFlag, " +
            "Auto.AutoNo, " +
            "MakeCardVouchersM.IsAutoUse,\n" +
            "TraderM.Code as TraderCode, " +
            "TraderM.Name as TraderName, " +
            "MakeCardVouchersD.StartDate, " +
            "MakeCardVouchersD.EndDate, " +
            "CardVouchersRuleM.UseRules " +
            "from MakeCardVouchersD " +
            "left outer join MakeCardVouchersM on MakeCardVouchersD.MakeCardVouchersM_FK=MakeCardVouchersM.MakeCardVouchersMOID " +
            "left outer join CardVouchersRuleM on MakeCardVouchersM.CardVouchersRuleM_FK=CardVouchersRuleM.CardVouchersRuleMOID " +
            "left outer join GoodsM on MakeCardVouchersM.GoodsM_FK = GoodsM.GoodsMOID " +
            "left outer join TraderM on MakeCardVouchersD.TraderM_FK = TraderM.TraderMOID " +
            "left outer join Auto on MakeCardVouchersD.Auto_FK = Auto.AutoOID " +
            "where TraderMOID=#{traderMOID} ) t where SentFlag='使用中'")
    Integer getDiscountInfo(@Param("traderMOID") String traderMOID);

    @Select("UPDATE TraderM SET Name=#{userName} WHERE (TraderMOID =#{traderMOID})")
    void updateUserNameInfo(@Param("userName") String userName, @Param("traderMOID") String tradermoid);

    @Select("UPDATE TraderM SET HeadPicUrl=#{headImg} WHERE (TraderMOID =#{traderMOID})")
    void updateUserHeadInfo(@Param("headImg") String headImg, @Param("traderMOID") String tradermoid);

    @Select("UPDATE TraderM SET WXOpenID = null WHERE TraderMOID =#{traderMOID} ")
    void setWxOpenIdIsNull(@Param("traderMOID") String tradermoid);
}